package com.tpp.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.tpp.pojo.baseVo.BaseResult;
import com.tpp.utils.JwtUtils;
import com.tpp.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class MyZuulFilter extends ZuulFilter {

    private static final List<String> urlList=new ArrayList<>();

    @Autowired
    RedisUtils redisUtils;
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        //哪些请求直接放行
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();

        String requestURI = request.getRequestURI();
        if (urlList.contains(requestURI)){
            return  false;
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //进入过滤器，通过request获取到header
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String token = request.getHeader("token");
        if (token!=null){
            //从redis中获取到 token
            boolean b = redisUtils.hasKey(token);
            if (b){
                Object o = redisUtils.get(token);
                //判断用户的权限等等等。。。
                JwtUtils jwtUtils = new JwtUtils();
                try {
                    jwtUtils.VerifyToekn(o.toString());
                }catch (Exception e){
                    HttpServletResponse response = currentContext.getResponse();
                    response.setCharacterEncoding("utf-8");
                    response.setContentType("application/json");
                    try {
                        response.getWriter().write(JSONObject.toJSONString(new BaseResult(0,"登录信息已失效！")));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }finally {
                        currentContext.setSendZuulResponse(false);
                    }
                }

            }else{
                HttpServletResponse response = currentContext.getResponse();
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json");
                try {
                    response.getWriter().write(JSONObject.toJSONString(new BaseResult(0,"用户token异常！")));
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    currentContext.setSendZuulResponse(false);
                }
            }


        }else{
            HttpServletResponse response = currentContext.getResponse();
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            try {
                response.getWriter().write(JSONObject.toJSONString(new BaseResult(0,"请登录")));
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                currentContext.setSendZuulResponse(false);
            }
        }
        return null;
    }
    public MyZuulFilter(){
        urlList.add("/tpp-cloud-user/user/validataEmail");
        urlList.add("/tpp-cloud-user/user/validataloginName");
        urlList.add("/tpp-cloud-user/user/getCode");
        urlList.add("/tpp-cloud-user/user/getInCode");
        urlList.add("/tpp-cloud-user/user/registry");
        urlList.add("/tpp-cloud-user/user/login");
        urlList.add("/tpp-cloud-user/user/findUserByToken");
        urlList.add("/tpp-cloud-user/user/upload");

        urlList.add("/tpp-cloud-information/email/sendMail");
        urlList.add("/tpp-cloud-information/information/getCode");

        urlList.add("/tpp-cloud-seat/seat/findAll");

        urlList.add("/tpp-cloud-movie/tppMovie/findByMovieWei");
        urlList.add("/tpp-cloud-movie/tppMovie/findByMovieHeat");
        urlList.add("/tpp-cloud-movie/tppMovie/findMovieScore");
        urlList.add("/tpp-cloud-movie/index/index");

        urlList.add("/tpp-cloud-es/search/search");
    }
}
